/**
 * Author: Todd Kerpelman
 * Date: 6/14/12
 * Time: 3:27 PM
 *
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package net.kerp.hangoutSpace {

import flash.external.ExternalInterface;

public class HangoutCommunicator {
  private var _peopleManager:PeopleManager;

  public function HangoutCommunicator(peopleManager:PeopleManager) {
    _peopleManager = peopleManager;
    if (ExternalInterface.available) {
      ExternalInterface.addCallback("tellFlashUsersInHangout", receiveUsersInHangout);
      ExternalInterface.addCallback("tellFlashUserAdded", receiveNewUser);
      ExternalInterface.addCallback("tellFlashUserRemoved", receiveDepartingUser);
      ExternalInterface.addCallback("tellFlashNewVolumes", receiveVolumeData);
    }
  }

  private function receiveDepartingUser(leavingUser:Object):void {
    _peopleManager.removeExistingUser(leavingUser)
  }

  private function receiveUsersInHangout(data:Array):void {
    trace("Got back users in hangout -- an array of "+ data.length + " items");
    _peopleManager.setAllUsers(data);

  }

  private function receiveNewUser(userObject:Object):void {
    _peopleManager.addNewUser(userObject);

  }

  private function receiveVolumeData(volumeData:Object):void {
    _peopleManager.processVolumeData(volumeData);
  }

  public function requestPeopleInHangout():void {
    if (ExternalInterface.available) {
      trace("Requesting users in hangout and adding callback listeners");
      ExternalInterface.call("flashIsReady")
    }

  }

  public function initApp():void {
    this.requestPeopleInHangout();
  }
}
}
